<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\model\MbtiAnswers;
use app\common\model\MbtiConfig;
use app\common\model\MbtiNews;
use app\common\model\MbtiQuestions;
use app\common\model\MbtiTests;
use think\db\exception\DbException;
use think\model\concern\Virtual;
use think\response\View;
use Ip2Region;

class Topic extends BaseController
{
    public function reports()
    {
        $data = [ 'category' => '','question' => ''];
        $query = new MbtiTests();
        $list = $query->with(['detail'])->order('id', 'desc')
            ->paginate(50);
        return \view('/admin/topic/reports',['list' => $list, 'data' => []]);
    }

    public function reportsEdit()
    {
        $id = input('id');
        $password = input('password');
        $check = MbtiTests::where('password', $password)->find();
        if (!$check) {
            return toJson(1,'密码已存在  ');
        }
        MbtiTests::where('id', $id)
            ->update(['password' => $password]);
    }


    /**
     * 题目列表
     * @return View
     * @throws DbException
     */
    public function show()
    {
        $data = [ 'category' => '','question' => ''];
        $query = new MbtiQuestions;
        if (request()->get()) {
            $data = request()->param();
            if (!empty($data['category'])) {
                $query = $query->where('category', $data['category']);
            }
            if (!empty($data['question'])) {
                $query = $query->where('question', 'like', '%' . $data['question'] . '%');
            }
        }
        $list = $query->order('id', 'desc')
            ->paginate([
                'list_rows' => 50, // 每页显示的记录数
                'query' => [
                    'category' => $data['category'],
                    'question' => $data['question'],
                ]
            ]);

        return view('./admin/topic', ['list' => $list, 'data' => $data]);
    }

    //题目编辑
    public function edit()
    {
        if (request()->isPost()) {
            $data = request()->param();
            // print_r($data);die;
            MbtiQuestions::where('id',$data['id'])->update($data);
            return toJson(0,'ok',null);
        }
        $id = input('id');
        $detail = MbtiQuestions::where('id',$id)->find();
        
        return view('./admin/topic/edit',['detail' =>$detail]);
    }

   /**
     * 资讯列表
     */
    public function news()
    {

        $data = [ 'title' => ''];

        $query = new MbtiNews();
        if (request()->get()) {
            $data = request()->param();
            if (!empty($data['title'])) {
                $query = $query->where('title', 'like', '%' . $data['title'] . '%');
            }
        }
        $list = $query->order('id', 'desc')
            ->paginate([
                     'list_rows' => 10, // 每页显示的记录数
                     'query' => [
                         'title' => $data['title'] ?? '',
                     ]
                 ]);
        return view('./admin/topic/news', ['list' => $list,'data' => $data]);

    }
    
    public function newsAdd()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = date('Y-m-d H:i:s');
            if(get_img_src($data['content'])){
                $data['is_img'] = 1;
            }
            MbtiNews::insert($data);
            return toJson(0,'ok',null);
        }
        return view('./admin/topic/news_add');
    }

    public function newsEdit()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $data['update_time'] = date('Y-m-d H:i:s');
            if(get_img_src($data['content'])){
                $data['is_img'] = 1;
            }
            MbtiNews::where('id',$data['id'])->update($data);
            return toJson(0,'ok',null);
        }
           $news =  MbtiNews::where('id',input('id'))->find();

        return view('./admin/topic/news_edit',['news'=>$news]);
    }

    public function newsDelete()
    {
        $id = request()->param('id');
        MbtiNews::where('id', $id)->delete();
        return toJson(0,'ok',null);

    }

    public function in_delete()
    {
        $ids = request()->param('ids');
        MbtiNews::whereIn('id', $ids)->delete();
        return toJson(0,'ok',null);
    }
}